<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登录的头部 -->
      <div class="avatar_box">
        <img src="../../public/favicon.ico" />
      </div>
      <!-- 登录表单 -->
      <!-- :model是自定义组件的一个属性，不是v-model -->
      <el-form ref="form" :rules="rulesForm" :model="user" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="user.username" prefix-icon="user"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="user.password" prefix-icon="lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="user.verityCode" prefix-icon="user" style="width: 50%;"></el-input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <img v-if="imageUrl" :src="imageUrl" alt="Image" @click="flushCode"/>
        </el-form-item>
        <div class="btns">
          <el-form-item class="btns">
            <el-button type="primary" @click="submitForm">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup name="Login">

  import { useRouter, useRoute } from 'vue-router'
  const router = useRouter()
  const route = useRoute()

  import { ref,onMounted } from 'vue'

  
  import { inject } from 'vue'
import { ElMessage } from 'element-plus';
  let $http = inject('$http');
  const form = ref();
let imageUrl=ref();
  const user = ref({
    
  });

  const rulesForm = ref({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      {
        min: 3,
        max: 15,
        message: '长度在 3 到 15 个字符',
        trigger: 'blur'
      }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
    ]
  });

  // 重置form表单
  const resetForm = function () {
    $http({method: "get", url: "/admin/resetAdminPassword"}).then(response=> {
      ElMessage.success(response.data.msg)
    })
  };

  // 提交数据
  const submitForm = function () {
    form.value.validate(async (valid) => {
      if (!valid) return
      $http({method: "post", url: "/admin/login",data: user.value})
          .then(response=> {
            if (response.data.code == '200') {
              console.log(response.data.data["admin-token"]);
            localStorage.setItem("admin-token", response.data.data["admin-token"]);
            ElMessage.success("登录成功")
            router.push("/home")
            }else{
              ElMessage.error(response.data.msg)
            }
          }).catch(error=> {
            ElMessage.error(response.data.msg)
          })
    })
  }
  let flushCode=function () {
    $http({
    method: 'get',
    url: '/admin/createCode',
    responseType: 'arraybuffer'
  }).then(response => {
    const blob = new Blob([response.data], { type: 'image/jpeg' });
    imageUrl.value = URL.createObjectURL(blob);
    //响应头里的数据
    user.value.verityId = response.headers['codeid'];
    console.log(user.value.verityId);
  })
  }
onMounted(()=>{
 flushCode()
})



</script>

<style scoped>
  .login_container {
    background-color: #2b4b6b;
    background-image: url('../assets/bg.png');
    background-size: cover;
    background-position: center;
    height: 100%;
  }

  .login_box {
    width: 400px;
    height: 250px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .avatar_box {
    height: 80px;
    width: 80px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
  }

  .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
</style>
